﻿using HBKY.Shop.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace HBKY.Shop.IRepository.Base
{
    public interface IRepository<TEntity>  where TEntity : class, new()
    {
        /// <summary>
        /// Db对象
        /// </summary>
        public ISqlSugarClient Db { get; }

        /// <summary>
        /// 实体查询对象
        /// </summary>
        ISugarQueryable<TEntity> Queryable { get; }

        /// <summary>
        /// 开启事务
        /// </summary>
        void BeginTran();

        /// <summary>
        /// 事务提交
        /// </summary>
        void Commit();

        /// <summary>
        /// 事务回滚
        /// </summary>
        void Rollback();

        /// <summary>
        /// Id 查询数据
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        Task<TEntity> GetById(int objId);

        /// <summary>
        /// Ids 查询数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        Task<List<TEntity>> GetListByIds(IEnumerable<int> ids);

        /// <summary>
        /// 获取实体集合
        /// </summary>
        /// <returns></returns>
        Task<List<TEntity>> GetList();

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<bool> Save(TEntity entity);

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<bool> Add(TEntity entity);

        /// <summary>
        /// 添加返回实体
        /// </summary>
        Task<TEntity> AddReturnEntity(TEntity entity);

        /// <summary>
        /// 添加 返回id 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        Task<int> AddReutrnIdentity(TEntity entity);

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        Task<int> AddBatch(List<TEntity> entities);

        /// <summary>
        /// 删除
        /// </summary>
        Task<bool> Delete(TEntity model);

        /// <summary>
        /// 删除
        /// </summary>
        Task<bool> DeleteById(dynamic id);

        /// <summary>
        /// 批量删除
        /// </summary>
        Task<bool> DeleteByIds(List<dynamic> ids);

        /// <summary>
        /// 表达式删除
        /// </summary>
        Task<bool> Delete(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 修改
        /// </summary>
        Task<bool> Update(TEntity entity);

        /// <summary>
        /// 指定字段修改
        /// </summary>
        Task<bool> Update(TEntity entity, Expression<Func<TEntity, object>> columns);

        // <summary>
        /// 指定字段更新 传入字段委托
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="columns"></param>
        /// <returns></returns>
        Task<bool> UpdateBatch(List<TEntity> entity, Expression<Func<TEntity, object>> columns);

        /// <summary>
        /// 指定字段修改、赋值
        /// </summary>
        Task<bool> Update(object id, Expression<Func<TEntity, object>> columns,
            Expression<Func<TEntity, bool>> setValueExpression);

        /// <summary>
        /// 指定字段修改、赋值
        /// </summary>
        Task<bool> Update(TEntity entity, params string[] columns);

        /// <summary>
        /// 指定字段修改、赋值
        /// </summary>
        Task<bool> UpdateBatch(List<TEntity> entities);

        /// <summary>
        /// 批量指定字段修改、赋值
        /// </summary>
        Task<bool> UpdateBatch(IEnumerable<int> ids, Expression<Func<TEntity, TEntity>> columns);

        /// <summary>
        /// 指定字段修改
        /// </summary>
        Task<bool> UpdateBatchFilterExist(List<TEntity> entity, Expression<Func<TEntity, object>> columns);

        /// <summary>
        /// 组合查询条件
        /// </summary>
        ISugarQueryable<TEntity> Where(Expression<Func<TEntity, bool>> expression);

        /// <summary>
        /// 组合查询条件
        /// </summary>
        ISugarQueryable<TEntity> WhereIF(bool isWhere, Expression<Func<TEntity, bool>> expression);
    }
}